<template>
  <div class="execution">
    <basic-container>
      <avue-crud ref="crud"
                 :page.sync="page"
                 :data="tableData"
                 :permission="permissionList"
                 :table-loading="tableLoading"
                 :option="tableOption"
                 @on-load="getList"
                 @search-change="searchChange"
                 @refresh-change="refreshChange"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 @row-update="handleUpdate"
                 @row-save="handleSave"
                 @row-del="rowDel">
        <template slot="showStatus" slot-scope="scope">
          <el-switch v-model="scope.row.showStatus" @change="handleChangeStatus(scope.row)" :active-value="1"
                     :inactive-value="0"></el-switch>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button @click="showDialog(scope.row,scope.index)" v-if="!scope.row.hasChildren" icon="el-icon-s-operation"
                     :size="scope.size" :type="scope.type">关联分类
          </el-button>
        </template>
      </avue-crud>
      <el-dialog
        title="关联分类"
        :visible.sync="dialogVisible"
        width="30%">
        <el-form style="float: left;" ref="form" :inline="true" label-width="60px">
          <el-form-item label="分类id" width="200">
            <el-cascader
              v-model="brandCategory.catelogId"
              :options="cascade"
              @change="handleChange"></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="commitBrandCategory()" type="success">添加</el-button>
          </el-form-item>
        </el-form>
        <el-table style="width: 100%" :data="brandCategoryTable" stripe border>
          <el-table-column
            prop="id"
            label="#"
            width="60">
          </el-table-column>
          <el-table-column
            prop="brandName"
            label="品牌名"
            width="120">
          </el-table-column>
          <el-table-column
            prop="catelogName"
            label="分类名"
            width="120">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="removeBrandCategory(scope.row)" size="small">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">关 闭</el-button>
        </span>
      </el-dialog>

    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  addBrandCategory,
  brandCategoryList,
  delBrandCategory
} from '@/api/brand'
import {tableOption} from '@/const/crud/brand'
import {mapGetters} from 'vuex'
import {fetchCidTree} from "@/api/category";

export default {
  name: 'brand',
  data() {
    return {
      searchForm: {},
      dialogVisible: false,
      cascade: [],
      brandCategory: {
        brandId: '',
        catelogId: ''
      },
      tableData: [],
      brandCategoryTable: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.product_brand_add, false),
        delBtn: this.vaildData(this.permissions.product_brand_del, false),
        editBtn: this.vaildData(this.permissions.product_brand_edit, false)
      };
    }
  },
  created() {
    this.getTreeCategory();
  },
  methods: {
    getList(page, params) {
      this.tableLoading = true
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    getTreeCategory() {
      fetchCidTree(3).then(res => {
        this.cascade = res.data.data;
      });
    },
    rowDel: function (row, index) {
      this.$confirm('是否确认删除ID为' + row.brandId, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.brandId)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    handleUpdate: function (row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    handleSave: function (row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    currentChange(current) {
      this.page.currentPage = current
    },
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    refreshChange() {
      this.getList(this.page)
    },
    handleChangeStatus(row) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        this.getList(this.page)
      }).catch(() => {

      });
    },
    showDialog(row, index) {
      this.brandCategory.brandId = row.brandId;
      brandCategoryList(row.brandId).then(response => {
        this.brandCategoryTable = response.data.data;
      });
      this.dialogVisible = true;

    },
    handleChange(value) {
      this.brandCategory.catelogId = value[2];
    },
    commitBrandCategory() {
      if (this.brandCategory.catelogId === '') {
        this.$message.warning('请先选择分类');
        return;
      }
      addBrandCategory(this.brandCategory).then(response => {
        if (response.data.code === 0) {
          this.$message.success(response.data.msg);
        } else {
          this.$message.error(response.data.msg);
        }
      }).finally(()=>{
        brandCategoryList(this.brandCategory.brandId).then(response => {
          this.brandCategoryTable = response.data.data;
        });
        this.brandCategory.catelogId = '';
      })
    },
    removeBrandCategory(row) {
      this.$confirm('是否确认移除【' + row.brandName + '】和【' + row.catelogName + '】的关联关系?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delBrandCategory(row.id)
      }).then(data => {
        this.$message.success('移除成功')
        brandCategoryList(row.brandId).then(response => {
          this.brandCategoryTable = response.data.data;
        });
      })
    }
  }
}
</script>
